package com.nightsoul.springboottest2.domain;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
	@Resource
	private MongoTemplate mongoTemplate;

	@Override
	public void save(User user) {
		this.mongoTemplate.save(user);
	}

	@Override
	public List<User> findAll() {
		Query query = new Query(Criteria.where("id").lt(0));
		return this.mongoTemplate.find(query, User.class);
	}

	@Override
	public User findByusername(String username) {
		Query query = new Query(Criteria.where("username").is(username));
		User user = this.mongoTemplate.findOne(query, User.class);
		return user;
	}

}
